/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.iuh.dhth7dlt.emailclient.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Friendless
 */
public class EmailAddress {

    public static String getEmails(String s) throws EmailExceptions {
        if(s.isEmpty())
            return "";
        Pattern p = Pattern.compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b",
                Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(s);
        StringBuilder sb = new StringBuilder();       
        while (m.find()) {
            String email = m.group();
            if (!Validations.Email(email)) {
                throw new EmailExceptions().set_error("Email nhập vào lỗi:" + email);
            }         
            sb.append(String.format("<%s>,", email));
        }
        if("".equals(sb.toString()))
            throw new EmailExceptions().set_error("Email nhập vào lỗi:" + s);
        return sb.toString().substring(0, sb.length() - 1);

    }
}
